package com.xy.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class MyConfigCenter implements Watcher {
    private static String zkConnUrl = "127.0.0.1:2181";
    //计数器对象
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    //连接对象
    private static ZooKeeper zooKeeper;

    private String url;
    private String username;
    private String password;

    public MyConfigCenter() {
        initValue();
    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getType() == Event.EventType.None) {
            switch (watchedEvent.getState()) {
                case SyncConnected:
                    System.out.println("连接成功");
                    countDownLatch.countDown();
                    break;
                case Disconnected:
                    System.out.println("断开连接");
                    break;
                case Expired:
                    System.out.println("会话过期");
                    break;
                case AuthFailed:
                    System.out.println("授权失败");
                    break;
            }
        } else if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
            initValue();
        }
    }

    public void initValue() {
        try {
            zooKeeper = new ZooKeeper(zkConnUrl, 5000, this);
            countDownLatch.await();
            this.url = new String(zooKeeper.getData("/config/url", true, null));
            this.username = new String(zooKeeper.getData("/config/username", true, null));
            this.password = new String(zooKeeper.getData("/config/password", true, null));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            MyConfigCenter myConfigCenter = new MyConfigCenter();
            for(int i = 0; i <= 10; i++) {
                Thread.sleep(3000);
                System.out.println("url:" + myConfigCenter.getUrl());
                System.out.println("username:" + myConfigCenter.getUsername());
                System.out.println("password:" + myConfigCenter.getPassword());
                System.out.println("#################");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
